package com.phone580.app.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.phone580.app.Constant;
import com.phone580.app.db.AppHelperDBManager;
import com.phone580.app.db.AppHelperDatabase.DownloadInfoDB;
import com.phone580.app.entity.DownloadInfoEntity;

public class DownloadInfoDao extends AppHelperDBManager {

	public DownloadInfoDao(Context context) {
		super(context);
	}
	
	private static final String[] columns = {
		DownloadInfoDB._ID,
		DownloadInfoDB.URL,
		DownloadInfoDB.FILE_NAME,
		DownloadInfoDB.FILE_PATH,
		DownloadInfoDB.FILE_SIZE,
		DownloadInfoDB.COMPLETE_SIZE,
		DownloadInfoDB.STATUS,
		DownloadInfoDB.ICON
	};

	public long addItem(DownloadInfoEntity entity) {
		ContentValues cv = new ContentValues();
		cv.put(DownloadInfoDB.URL, entity.getUrl());
		cv.put(DownloadInfoDB.FILE_NAME, entity.getFileName());
		cv.put(DownloadInfoDB.FILE_PATH, entity.getFilePath());
		cv.put(DownloadInfoDB.FILE_SIZE, entity.getFileSize());
		cv.put(DownloadInfoDB.COMPLETE_SIZE, entity.getCompleteSize());
		cv.put(DownloadInfoDB.STATUS, entity.getStatus());
		cv.put(DownloadInfoDB.ICON, entity.getIcon());
		open();
		long id = mSQLiteDatabase.insert(DownloadInfoDB.TABLE_NAME, null, cv);
		close();
		return id;
	}
	
	public void updateItem(SQLiteDatabase sql, DownloadInfoEntity entity) {
		ContentValues cv = new ContentValues();
		cv.put(DownloadInfoDB.URL, entity.getUrl());
		cv.put(DownloadInfoDB.FILE_NAME, entity.getFileName());
		cv.put(DownloadInfoDB.FILE_PATH, entity.getFilePath());
		cv.put(DownloadInfoDB.FILE_SIZE, entity.getFileSize());
		cv.put(DownloadInfoDB.COMPLETE_SIZE, entity.getCompleteSize());
		cv.put(DownloadInfoDB.STATUS, entity.getStatus());
		cv.put(DownloadInfoDB.ICON, entity.getIcon());
		sql.update(DownloadInfoDB.TABLE_NAME, cv, DownloadInfoDB._ID + "=" + entity.getId(), new String[]{});
	}
	
	public void updateStatus(long id, int status) {
		ContentValues cv = new ContentValues();
		cv.put(DownloadInfoDB.STATUS, status);
		open();
		mSQLiteDatabase.update(DownloadInfoDB.TABLE_NAME, cv, DownloadInfoDB._ID + " == " + id, new String[]{});
		close();
	}
	
	public void updateStatus(DownloadInfoEntity info) {
		ContentValues cv = new ContentValues();
		cv.put(DownloadInfoDB.STATUS, info.getStatus());
		cv.put(DownloadInfoDB.FILE_SIZE, info.getFileSize());
		cv.put(DownloadInfoDB.COMPLETE_SIZE, info.getCompleteSize());
		open();
		mSQLiteDatabase.update(DownloadInfoDB.TABLE_NAME, cv, DownloadInfoDB._ID + " == " + info.getId(), new String[]{});
		close();
	}
	
	public void updateCompleteSize(String url, int size) {
		ContentValues cv = new ContentValues();
		cv.put(DownloadInfoDB.COMPLETE_SIZE, size);
		open();
		mSQLiteDatabase.update(DownloadInfoDB.TABLE_NAME, cv, DownloadInfoDB.URL + "='" + url + "'", new String[]{});
		close();
	}
	
	public void updateCompleteSize(long id, int size) {
		ContentValues cv = new ContentValues();
		cv.put(DownloadInfoDB.COMPLETE_SIZE, size);
		open();
		mSQLiteDatabase.update(DownloadInfoDB.TABLE_NAME, cv, DownloadInfoDB._ID + "=" + id, new String[]{});
		close();
	}
	
//	public void updateFileSize(String url, int fileSize) {
//		ContentValues cv = new ContentValues();
//		cv.put(DownloadInfoDB.FILE_SIZE, fileSize);
//		open();
//		mSQLiteDatabase.update(DownloadInfoDB.TABLE_NAME, cv, DownloadInfoDB.URL + "='" + url + "'", new String[]{});
//		close();
//	}
	
//	public void updateFileSize(long id, int fileSize) {
//		ContentValues cv = new ContentValues();
//		cv.put(DownloadInfoDB.FILE_SIZE, fileSize);
//		open();
//		mSQLiteDatabase.update(DownloadInfoDB.TABLE_NAME, cv, DownloadInfoDB._ID + "=" + id, new String[]{});
//		close();
//	}
	
	public void remove(String url) {
		open();
		mSQLiteDatabase.delete(DownloadInfoDB.TABLE_NAME, DownloadInfoDB.URL + "='" + url + "'", new String[]{});
		close();
	}
	
	public void remove(long id) {
		open();
		mSQLiteDatabase.delete(DownloadInfoDB.TABLE_NAME, DownloadInfoDB._ID + "=" + id, new String[]{});
		close();
	}
	
	public List<DownloadInfoEntity> fetch() {
		List<DownloadInfoEntity> list = new ArrayList<DownloadInfoEntity>();
		open();
		Cursor c = mSQLiteDatabase.query(DownloadInfoDB.TABLE_NAME, columns, null, null, null, null, DownloadInfoDB._ID + " desc");
		while (c.moveToNext()) {
			DownloadInfoEntity entity = new DownloadInfoEntity();
			entity.setId(c.getLong(c.getColumnIndex(DownloadInfoDB._ID)));
			entity.setUrl(c.getString(c.getColumnIndex(DownloadInfoDB.URL)));
			entity.setFileName(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_NAME)));
			entity.setFilePath(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_PATH)));
			entity.setFileSize(c.getInt(c.getColumnIndex(DownloadInfoDB.FILE_SIZE)));
			entity.setCompleteSize(c.getInt(c.getColumnIndex(DownloadInfoDB.COMPLETE_SIZE)));
			entity.setIcon(c.getString(c.getColumnIndex(DownloadInfoDB.ICON)));
//			entity.setFinishDownload(c.getInt(c.getColumnIndex(DownloadInfoDB.FINISH)) == DownloadInfoDB.FINISHED ? true : false);
			entity.setStatus(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)));
			list.add(entity);
		}
		c.close();
		close();
		return list;
	}
	
	public List<DownloadInfoEntity> fetchFinished() {
		List<DownloadInfoEntity> list = new ArrayList<DownloadInfoEntity>();
		open();
		Cursor c = mSQLiteDatabase.query(
				DownloadInfoDB.TABLE_NAME, 
				columns, 
				DownloadInfoDB.STATUS + " == " + Constant.DOWNLOAD_FINISH, 
				null, null, null, DownloadInfoDB._ID + " desc");
		
		while (c.moveToNext()) {
			DownloadInfoEntity entity = new DownloadInfoEntity();
			entity.setId(c.getLong(c.getColumnIndex(DownloadInfoDB._ID)));
			entity.setUrl(c.getString(c.getColumnIndex(DownloadInfoDB.URL)));
			entity.setFileName(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_NAME)));
			entity.setFilePath(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_PATH)));
			entity.setFileSize(c.getInt(c.getColumnIndex(DownloadInfoDB.FILE_SIZE)));
			entity.setCompleteSize(c.getInt(c.getColumnIndex(DownloadInfoDB.COMPLETE_SIZE)));
			entity.setIcon(c.getString(c.getColumnIndex(DownloadInfoDB.ICON)));
//			entity.setFinishDownload(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)) == DownloadInfoDB.FINISHED ? true : false);
			entity.setStatus(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)));
			list.add(entity);
		}
		c.close();
		close();
		return list;
	}
	
	public List<DownloadInfoEntity> fetchUnFinished() {
		List<DownloadInfoEntity> list = new ArrayList<DownloadInfoEntity>();
		open();
		Cursor c = mSQLiteDatabase.query(
				DownloadInfoDB.TABLE_NAME, 
				columns, 
				DownloadInfoDB.STATUS + " != " + Constant.DOWNLOAD_FINISH, 
				null, null, null, DownloadInfoDB._ID + " asc");
		
		while (c.moveToNext()) {
			DownloadInfoEntity entity = new DownloadInfoEntity();
			entity.setId(c.getLong(c.getColumnIndex(DownloadInfoDB._ID)));
			entity.setUrl(c.getString(c.getColumnIndex(DownloadInfoDB.URL)));
			entity.setFileName(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_NAME)));
			entity.setFilePath(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_PATH)));
			entity.setFileSize(c.getInt(c.getColumnIndex(DownloadInfoDB.FILE_SIZE)));
			entity.setCompleteSize(c.getInt(c.getColumnIndex(DownloadInfoDB.COMPLETE_SIZE)));
			entity.setIcon(c.getString(c.getColumnIndex(DownloadInfoDB.ICON)));
//			entity.setFinishDownload(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)) == DownloadInfoDB.FINISHED ? true : false);
			entity.setStatus(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)));
			list.add(entity);
		}
		c.close();
		close();
		return list;
	}
	
	public DownloadInfoEntity fetchByURL (String url) {
		open();
		Cursor c = mSQLiteDatabase.query(DownloadInfoDB.TABLE_NAME, columns, DownloadInfoDB.URL + " = '" + url + "'", null, null, null, null);
		DownloadInfoEntity entity = null;
		if (c.moveToNext()) {
			entity = new DownloadInfoEntity();
			entity.setId(c.getLong(c.getColumnIndex(DownloadInfoDB._ID)));
			entity.setUrl(c.getString(c.getColumnIndex(DownloadInfoDB.URL)));
			entity.setFileName(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_NAME)));
			entity.setFilePath(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_PATH)));
			entity.setFileSize(c.getInt(c.getColumnIndex(DownloadInfoDB.FILE_SIZE)));
			entity.setIcon(c.getString(c.getColumnIndex(DownloadInfoDB.ICON)));
			entity.setCompleteSize(c.getInt(c.getColumnIndex(DownloadInfoDB.COMPLETE_SIZE)));
//			entity.setFinishDownload(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)) == DownloadInfoDB.FINISHED ? true : false);
			entity.setStatus(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)));
		}
		c.close();
		close();
		return entity;
	}
	
	public DownloadInfoEntity fetchById (long id) {
		open();
		Cursor c = mSQLiteDatabase.query(DownloadInfoDB.TABLE_NAME, columns, DownloadInfoDB._ID + " = s" + id, null, null, null, null);
		DownloadInfoEntity entity = null;
		if (c.moveToNext()) {
			entity = new DownloadInfoEntity();
			entity.setId(c.getLong(c.getColumnIndex(DownloadInfoDB._ID)));
			entity.setUrl(c.getString(c.getColumnIndex(DownloadInfoDB.URL)));
			entity.setFileName(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_NAME)));
			entity.setFilePath(c.getString(c.getColumnIndex(DownloadInfoDB.FILE_PATH)));
			entity.setFileSize(c.getInt(c.getColumnIndex(DownloadInfoDB.FILE_SIZE)));
			entity.setIcon(c.getString(c.getColumnIndex(DownloadInfoDB.ICON)));
			entity.setCompleteSize(c.getInt(c.getColumnIndex(DownloadInfoDB.COMPLETE_SIZE)));
//			entity.setFinishDownload(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)) == DownloadInfoDB.FINISHED ? true : false);
			entity.setStatus(c.getInt(c.getColumnIndex(DownloadInfoDB.STATUS)));
		}
		c.close();
		close();
		return entity;
	}
	
	/**
	 * 下载URL是否存在
	 * @param url
	 * @return true存在 false不存在
	 */
	public int isExist(String url) {
		open();
		Cursor c = mSQLiteDatabase.query(DownloadInfoDB.TABLE_NAME, columns, DownloadInfoDB.URL + " = '" + url + "'", null, null, null, null);
		int count = c.getCount();
		c.close();
		close();
		return count;
	}
	
//	public boolean isDownloading(String url) {
//		open();
//		Cursor c = mSQLiteDatabase.query(DownloadInfoDB.TABLE_NAME, columns, DownloadInfoDB.URL + " = '" + url + "' and " + DownloadInfoDB.STATUS + " == " + UNFINISH, null, null, null, null);
//		int count = c.getCount();
//		c.close();
//		close();
//		return count > 0 ? true : false;
//	}
}
